'use strict';

const Controller = require('egg').Controller;
const fs = require('fs');
// const xlsx = require("node-xlsx");
// const list = xlsx.parse("./files/data.xls"); // 需要 转换的excel文件
const areaList = require('../../files/areaData');
class AreaController extends Controller {
  async index() {
    const { ctx } = this;
    let dataList = [];
    areaList.map((a_item) => {
      if (!a_item.parent_code) { // 获取全部省
        // 省下面的城市
        a_item.children = this.getDistListBypCode(a_item.code); 
        a_item.children.map((c_item) => {
          // 城市下面的区
          c_item.children = this.getDistListBypCode(c_item.code);
        })
        dataList.push(a_item)
      }
    });
    ctx.body = {
      data: dataList
    }
  };
  // 根据父级id获取子集数据集合 
  getDistListBypCode(p_code){
    let children = [];
    areaList.map((c_item) => {
      if (c_item.parent_code == p_code) {
        children.push(c_item)
      }
    })
    return children;
  };

  getFileData(filePath,encoding = 'utf8'){
    return new Promise((resolve,reject) => {
      fs.readFile(filePath,encoding,function (err,data) {
        if (err) {
          resolve(err); 
        } else {
          resolve(data);
        }
      })
    })
  }
}


module.exports = AreaController;
